package com.xtwy.config;

import com.xtwy.cache.CacheMessageListener;
import com.xtwy.cache.RedisEhcacheCacheManager;
import com.xtwy.properties.RedisEhcacheProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.io.Serializable;

/**
 * 类的描述：
 * CacheRedisEhcacheAutoConfiguration加上（yml没有配置或者配置为false，二级缓存都不起作用）：
 * @ConditionalOnProperty(name = "cache.use2L", havingValue = "true", matchIfMissing = false)
 * @作者: 侯建军
 * @邮箱: 474177818@qq.com
 * @博客: https://blog.csdn.net/marquis888
 * @创建日期: 2018年12月09日  14:59
 * @版本: v1.0
 * @jdk版本使用: JDK1.8
 */
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
//二级缓存和一级缓存切换:true 启用二级缓存，false禁用二级缓存
@ConditionalOnProperty(name = "cache.use2L", havingValue = "true", matchIfMissing = false)
@EnableConfigurationProperties(RedisEhcacheProperties.class)
@Slf4j
public class CacheRedisEhcacheAutoConfiguration {

    @Autowired
    private RedisEhcacheProperties redisEhcacheProperties;

    /**
     * Redis+Ehcache缓存管理
     * @param redisTemplate
     * @return
     */
    @Bean
    public RedisEhcacheCacheManager cacheManager(RedisTemplate<Object, Object> redisTemplate) {
        return new RedisEhcacheCacheManager(redisEhcacheProperties, redisTemplate);
    }

    /**
     * RedisMessage 监听
     * @param redisTemplate
     * @param redisEhcacheCacheManager
     * @return
     */
    @Bean
    @ConditionalOnBean(RedisEhcacheCacheManager.class)
    public RedisMessageListenerContainer redisMessageListenerContainer(RedisTemplate<Object, Object> redisTemplate,
                                                                       RedisEhcacheCacheManager redisEhcacheCacheManager) {
        RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
        redisMessageListenerContainer.setConnectionFactory(redisTemplate.getConnectionFactory());
        CacheMessageListener cacheMessageListener = new CacheMessageListener(redisTemplate, redisEhcacheCacheManager);
       //添加缓存监听器
        log.info("3、添加缓存监听到Redis");
        redisMessageListenerContainer.addMessageListener(cacheMessageListener, new ChannelTopic(redisEhcacheProperties.getRedis().getTopic()));
        return redisMessageListenerContainer;
    }
}

